package zhongbo.java.air.dictionary.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import zhongbo.java.air.common.util.AssertUtil;
import zhongbo.java.air.common.vo.PageVo;
import zhongbo.java.air.dictionary.mapper.AirCraftMapper;
import zhongbo.java.air.dictionary.model.AirCraft;
import zhongbo.java.air.dictionary.service.IAirCraftService;

import java.util.Date;

/**
 * 说明：
 *
 * @author 钟波dellNotebook
 * @since 2025年04月01日 星期二 07:35:10
 */
@Service
@Slf4j
public class AirCraftServiceImpl implements IAirCraftService<AirCraft> {

    @Autowired
    private AirCraftMapper airCraftMapper;

    @Override
    public AirCraft add(AirCraft airCraft) {
        validAirCraft(airCraft);
        AssertUtil.isNull(airCraft.getId(), "实体id不能填写");
        int countByIcao = airCraftMapper.selectCountByIcao(airCraft.getIcaoCode());
        if (countByIcao > 0) {
            throw new RuntimeException("ICAO码重复");
        }

        airCraft.setCreateTime(new Date());
        airCraft.setUpdateTime(new Date());
        int insert = airCraftMapper.insert(airCraft);
        if (insert > 0) {
            return airCraft;
        }
        throw new RuntimeException("新增实体失败");
    }

    /**
     * 通用验证实体信息
     */
    private void validAirCraft(AirCraft airCraft) {
        if (airCraft == null) {
            throw new RuntimeException("实体信息不能为空");
        }
        AssertUtil.maxLength(airCraft.getIcaoCode(), 6, "ICAO码不能超过6位");
        AssertUtil.notBlank(airCraft.getIcaoCode(), "ICAO码不能为空");
        AssertUtil.maxLength(airCraft.getRegistration(), 20, "飞机注册号超过20位");
    }

    @Override
    public String delete(String airCraftId) {
        if (StrUtil.isBlank(airCraftId)) {
            throw new RuntimeException("实体id不能为空");
        }
        int delete = airCraftMapper.deleteById(airCraftId);
        if (delete > 0) {
            return airCraftId;
        }
        throw new RuntimeException("删除实体失败");
    }

    @Override
    public AirCraft edit(AirCraft edit) {
        validAirCraft(edit);
        String airCraftId = edit.getId();
        if (StrUtil.isBlank(airCraftId)) {
            throw new RuntimeException("实体id不能为空");
        }
        AirCraft one = findOne(airCraftId);
        if (one == null) {
            throw new RuntimeException("实体不存在");
        }
        edit.setUpdateTime(new Date());
        int update = airCraftMapper.updateById(edit);
        if (update > 0) {
            return edit;
        }
        throw new RuntimeException("修改实体失败");
    }

    @Override
    public AirCraft findOne(String airCraftId) {
        if (StrUtil.isBlank(airCraftId)) {
            throw new RuntimeException("实体id不能为空");
        }
        return airCraftMapper.selectById(airCraftId);
    }

    @Override
    public PageVo<AirCraft> findPage(Integer page, Integer pageSize, String keyword, String fields, String oderByField, Boolean orderByAsc) {
        QueryWrapper<AirCraft> queryWrapper = new QueryWrapper<>();
        // 检测是否有排序
        if (StrUtil.isNotBlank(oderByField)) {
            // 检测是正序还是倒序
            boolean isOrderByAsc = Boolean.TRUE.equals(orderByAsc);
            if (isOrderByAsc) {
                // 结果正序排列
                queryWrapper.orderByAsc(StrUtil.toUnderlineCase(oderByField));
            } else {
                // 否则结果倒序排列
                queryWrapper.orderByDesc(StrUtil.toUnderlineCase(oderByField));
            }
        }
        // 支持指定字段模糊搜索keyword
        if (StrUtil.isNotBlank(keyword) && StrUtil.isNotBlank(fields)) {
            String[] fieldsArray = fields.split(",");
            for (String field : fieldsArray) {
                queryWrapper.or().like(StrUtil.toUnderlineCase(field), keyword);
            }
        }
        Page<AirCraft> pageParam = new Page<>(page, pageSize);
        Page<AirCraft> airCraftPage = airCraftMapper.selectPage(pageParam, queryWrapper);

        return new PageVo<>(airCraftPage);
    }

    private void validPassWord(String passWord) {
        if (StrUtil.isBlank(passWord)) {
            throw new RuntimeException("密码不能为空");
        }
    }

    private void validAirCraftName(String airCraftName) {
        if (StrUtil.isBlank(airCraftName)) {
            throw new RuntimeException("实体名不能为空");
        }
    }

}
